package com.neusoft.databus.policy.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.databus.policy.dto.PolicyRefResourceDTO;
import com.neusoft.databus.policy.model.PolicyRefResource;
import com.neusoft.databus.policy.repository.PolicyRefResourceRepository;

/**
 * 策略资源关联类Converter
 * 
 * @author naxiang
 */
@Component
public class PolicyRefResourceConverter extends AbstractConverter<PolicyRefResource, PolicyRefResourceDTO> {
    @Autowired
    private PolicyRefResourceRepository policyRefResourceRepository;

    @Override
    public PolicyRefResource toModel(PolicyRefResourceDTO dto) {
        PolicyRefResource model;
        if (dto.isNew()) {
            model = new PolicyRefResource();
        } else {
            model = this.policyRefResourceRepository.getOne(dto.getId());
        }
        model.setResourceCode(dto.getResourceCode());
        return model;
    }

    @Override
    public PolicyRefResourceDTO toDTO(PolicyRefResource model, boolean forListView) {
        PolicyRefResourceDTO dto = new PolicyRefResourceDTO();
        dto.setId(model.getId());
        dto.setResourceCode(model.getResourceCode());
        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());
        return dto;
    }

    public PolicyRefResource toModel(String resourceCode) {
        PolicyRefResource model = new PolicyRefResource();
        model.setResourceCode(resourceCode);
        return model;
    }
}
